//
//  GQHSuppliesView.m
//  Xanthium
//
//  Created by GuanQinghao on 17/03/2018.
//  Copyright © 2018 GuanQinghao. All rights reserved.
//

#import "GQHSuppliesView.h"
#import "GQHCustomCell.h"
#import "GQHHeader.h"

@interface GQHSuppliesView () <UITableViewDelegate,UITableViewDataSource,GQHSuppliesCellDelegate>

@end


@implementation GQHSuppliesView

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        
        [self loadLocalData];
        [self loadUserInterface];
    }
    return self;
}

#pragma mark --Data
- (void)loadLocalData {
    
}

#pragma mark --View
- (void)loadUserInterface {
    
    _qh_tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
    _qh_tableView.delegate = self;
    _qh_tableView.dataSource = self;
    _qh_tableView.backgroundColor = kWhiteColor;
    _qh_tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    _qh_tableView.showsVerticalScrollIndicator = NO;
    if (@available(iOS 11.0, *)) {
        
        _qh_tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
    }
    [self addSubview:_qh_tableView];
    [_qh_tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.mas_equalTo(self.qh_statusBarHeight + self.qh_navigationBarHeight);
        make.left.and.right.mas_equalTo(self);
        make.bottom.mas_equalTo(self);
    }];
}

#pragma mark --Delegate

- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return UITableViewCellEditingStyleDelete;
}

//先要设Cell可编辑
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    return YES;
}

- (NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return @"删除";
}

//点击删除
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    //在这里实现删除操作
    GQHFreightModel *freightModel = _qh_freightModelArray[indexPath.row];
    [self deleteData:freightModel.qh_freightID];
    //删除数据，和删除动画
    [self.qh_freightModelArray removeObjectAtIndex:indexPath.row];
    [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:[NSIndexPath indexPathForRow:indexPath.row inSection:0]] withRowAnimation:UITableViewRowAnimationTop];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _qh_freightModelArray.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    return 150.0f;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    static NSString *identifier = @"aCell";
    GQHSuppliesCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if (!cell) {
        
        cell = [[GQHSuppliesCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
    }
    
    cell.qh_delegate = self;
    cell.qh_checkQuotationButton.tag = indexPath.row;
    cell.qh_onSaleButton.tag = indexPath.row;
    
    GQHFreightModel *freightModel = _qh_freightModelArray[indexPath.row];
    
    cell.qh_routeLabel.text = [NSString stringWithFormat:@"%@-%@",freightModel.qh_departure,freightModel.qh_destination];
    cell.qh_contentLabel.text = [NSString stringWithFormat:@"%@/%@/%@",freightModel.qh_freightName,freightModel.qh_modelWanted,freightModel.qh_lengthWanted];
    cell.qh_loadingTimeLabel.text = [NSString stringWithFormat:@"装车时间:%@",freightModel.qh_loadingTime];
    [cell.qh_checkQuotationButton setTitle:[NSString stringWithFormat:@"%d人报价, 已选择%d人",freightModel.qh_quotationCount,freightModel.qh_agreementCount] forState:UIControlStateNormal];
    if ([freightModel.qh_state isEqualToString:@"01"]) {
        /// 发布中
        [cell.qh_onSaleButton setTitle:@"下架货源" forState:UIControlStateNormal];
    } else {
        /// 下架
        [cell.qh_onSaleButton setTitle:@"重新发布" forState:UIControlStateNormal];
    }
    /**
     @property (nonatomic, strong) UILabel *qh_routeLabel;
     @property (nonatomic, strong) UILabel *qh_contentLabel;
     @property (nonatomic, strong) UILabel *qh_loadingTimeLabel;
     @property (nonatomic, strong) UILabel *qh_quoteLabel;
     @property (nonatomic, strong) UIButton *qh_onSaleButton;
     
     
     @property (nonatomic, copy) NSString *qh_departureID;// 起始地行政编码
     @property (nonatomic, copy) NSString *qh_departure;// 起始地
     @property (nonatomic, copy) NSString *qh_destinationID;// 目的地行政编码
     @property (nonatomic, copy) NSString *qh_destination;// 目的地
     @property (nonatomic, copy) NSString *qh_freightID;// 货源ID
     @property (nonatomic, copy) NSString *qh_normalLength;// 车辆标准车长
     @property (nonatomic, copy) NSString *qh_modelCode;// 车型编码
     @property (nonatomic, copy) NSString *qh_model;// 车型
     @property (nonatomic, copy) NSString *qh_freightName;// 货物名称
     @property (nonatomic, copy) NSString *qh_freightType;// 货物类型
     @property (nonatomic, copy) NSString *qh_freightTypeID;// 货物类型编码
     @property (nonatomic, copy) NSString *qh_note;// 备注
     @property (nonatomic, assign) BOOL qh_followed;// 熟人 (关注货主)
     @property (nonatomic, copy) NSString *qh_postTime;// 发布时间
     @property (nonatomic, copy) NSString *qh_posterID;// 发布人ID
     @property (nonatomic, copy) NSString *qh_posterName;// 发布人姓名
     @property (nonatomic, copy) NSString *qh_posterMobile;// 发布人电话
     @property (nonatomic, copy) NSString *qh_posterType;// 发布人类型
     @property (nonatomic, assign) CGFloat qh_freightWeight;// 货物重量
     @property (nonatomic, assign) CGFloat qh_freightVolume;// 货物体积
     @property (nonatomic, assign) CGFloat qh_freightNumber;// 货物数量
     @property (nonatomic, assign) CGFloat qh_freightUnitPrice;// 货物单价
     @property (nonatomic, copy) NSString *qh_state;// 上架状态
     @property (nonatomic, copy) NSString *qh_loadingTime;// 装车时间
     @property (nonatomic, copy) NSString *qh_loader;// 装货人
     @property (nonatomic, copy) NSString *qh_loaderMobile;// 装货人电话
     @property (nonatomic, assign) CGFloat qh_mileage;// 公里数
     @property (nonatomic, assign) CGFloat qh_score;// 货主评分
     @property (nonatomic, assign) CGFloat qh_freightWeightRemained;// 剩余货物重量
     @property (nonatomic, assign) int qh_quotationCount;// 报价数目
     @property (nonatomic, assign) int qh_agreementCount;// 协议数目
     @property (nonatomic, copy) NSString *qh_avatar;// 头像
     @property (nonatomic, copy) NSString *qh_enterpriseName;// 企业名称
     
     */
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    if ([_qh_delegate respondsToSelector:@selector(qh_selectedRowWithTableView:atIndexPath:)]) {
        
        [_qh_delegate qh_selectedRowWithTableView:tableView atIndexPath:indexPath];
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    
    return CGFLOAT_MIN;
}

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    
    return [UIView new];
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    
    return CGFLOAT_MIN;
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    
    return [UIView new];
}

- (void)qh_checkQutation:(UIButton *)sender {
    DLog(@"查看报价情况");
    
    if ([_qh_delegate respondsToSelector:@selector(qh_checkQutation:)]) {
        
        [_qh_delegate qh_checkQutation:sender];
    }
}

- (void)qh_onSaleOrNot:(UIButton *)sender {
    DLog(@"上架或下架货源 Tag:%ld", (long)sender.tag);
    if ([_qh_delegate respondsToSelector:@selector(qh_onSaleOrNot:)]) {
        
        [_qh_delegate qh_onSaleOrNot:sender];
    }
}

- (void)deleteData:(NSString *)sourceID
{
    NSString *URLString = [[NSString qh_APIURLString] stringByAppendingString:@"/buSources/1/delete"];
    [GQHNetHelper qh_requestPOST:URLString parameters:@{@"sourcesId":sourceID} success:^(NSNumber *code, NSString *message, NSDictionary *data) {
        [self showToastWithText:message];
    } failure:^(NSError *error) {
        
    }];
}

@end
